package com.huawei.app.demo.service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.huawei.app.demo.util.LogUtils;

/**
 * 智能安装服务
 *
 * @since 2021-02-01
 */
public class InstallPkgService extends AccessibilityService {

    private static final String TAG = InstallPkgService.class.getSimpleName();
    private Set<String> actionSet = new HashSet<>();
    private Map<Integer, Boolean> handledMap = new HashMap<>();

    public InstallPkgService() {
        actionSet.add("INSTALL");
        actionSet.add("OPEN");
        actionSet.add("安装");
        actionSet.add("打开");
    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        LogUtils.getInstance().i(TAG,"onServiceConnected");
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        LogUtils.getInstance().i(TAG,"onRebind");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        LogUtils.getInstance().i(TAG,"onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onInterrupt() {
        LogUtils.getInstance().i(TAG,"onInterrupt");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.getInstance().i(TAG,"onDestroy");
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        AccessibilityNodeInfo nodeInfo = event.getSource();
        if (nodeInfo == null) {
            return;
        }

        int eventType = event.getEventType();
        if (eventType != AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED &&
                eventType != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            return;
        }
        if (handledMap.get(event.getWindowId()) != null) {
            LogUtils.getInstance().i(TAG,"repeat window id " + event.getWindowId() + ", nodeText=" + nodeInfo.getText());
            return;
        }

        LogUtils.getInstance().i(TAG,"first window id " + event.getWindowId() + ", nodeText=" + nodeInfo.getText());
        boolean handled = iterateNodesAndHandle(nodeInfo);
        if (handled) {
            handledMap.put(event.getWindowId(), true);
        }
    }

    private boolean iterateNodesAndHandle(AccessibilityNodeInfo nodeInfo) {
        LogUtils.getInstance().i(TAG,"nodeType: " + nodeInfo.getClassName()
                + ", nodeText: " + nodeInfo.getText()
                + ", childCount: " + nodeInfo.getChildCount());

        if ("android.widget.Button".equals(nodeInfo.getClassName())) {
            String nodeContent = nodeInfo.getText().toString();
            if (actionSet.contains(nodeContent)) {
                nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                return true;
            }
        } else if ("android.widget.ScrollView".equals(nodeInfo.getClassName())) {
            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
        }

        int childCount = nodeInfo.getChildCount();
        for (int i = 0; i < childCount; i++) {
            AccessibilityNodeInfo childNodeInfo = nodeInfo.getChild(i);
            if (iterateNodesAndHandle(childNodeInfo)) {
                return true;
            }
        }
        return false;
    }
}
